package com.hd.service.activiti;

import java.util.LinkedHashSet;
import java.util.Set;

import org.activiti.engine.impl.pvm.delegate.ActivityExecution;
import org.springframework.stereotype.Component;

import com.hd.commons.exception.MyException;
import com.hd.util.JsonUtil;
import com.hd.util.StringUtils;

/**
 * 设置会签人员信息
 * 
 * @author hzhh123 2018年8月18日下午4:07:15
 */
@SuppressWarnings("unchecked")
@Component("counterSignService")
public class CounterSignService {
	/**
	 * 获得会签任务中的人员计算集合
	 * 
	 * @param execution
	 * @return
	 */
	public Set<String> getUsers(ActivityExecution execution) {
		Set<String> userIds = new LinkedHashSet<String>();
		String nodeId = execution.getActivity().getId();
		// 判断是否会签回退
		Object signUserIds = execution.getVariable("signUserIds_" + nodeId);
		System.out.println(">>>>>>>>>>>>>>>>>>" + JsonUtil.toJson(signUserIds));
		System.out.println(">>>>>>>>>>>>>>>>>>流程实例ID=" + execution.getProcessInstanceId());
		System.out.println("流程定义=" + execution.getActivity().getProcessDefinition());
		System.out.println("任务名称=" + execution.getCurrentActivityName());
		System.out.println("任务Key=" + nodeId);
		if (StringUtils.isNotNull(signUserIds)) {
			userIds.addAll((Set<String>) signUserIds);
		} else {
			userIds.add("1");
			userIds.add("2");
			userIds.add("3");
			userIds.add("4");
			// 查询数据库
		}
		String name = (String) execution.getActivity().getProperty("name");
		if (userIds.size() == 0) {
			throw new MyException("会签节点[" + name + "]没有设置执行人员，请联系管理员！");
		}
		return userIds;
	}

	/**
	 * 会签是否计算完成
	 * 
	 * @param execution
	 * @return
	 */
	public boolean isComplete(ActivityExecution execution) {
		// 完成会签的次数
		Integer completeCounter = (Integer) execution.getVariable("nrOfCompletedInstances");
		// 总循环次数
		Integer instanceOfNumbers = (Integer) execution.getVariable("nrOfInstances");

		String solId = (String) execution.getVariable("solId");
		String nodeId = execution.getActivity().getId();
		// TODO

		return false;

	}
}
